//
//  StatusPictureView.swift
//  个人微博
//
//  Created by guo3920591 on 16/4/22.
//  Copyright © 2016年 guo3920591. All rights reserved.
//

import UIKit
//选中照片的通知名
let XLSelectPictureNotication = "XLSelectPictureNotication"
//选中的索引
let XLPictureSelectKey = "XLPictureSelectKey"
let XLPictureUrlKey = "XLPictureUrlKey"

class StatusPictureView: UIView {
     /// 设置图片的内容
    var pic_urls:[StatusPicturesModel]?{
        
        didSet{
            
            for iv in subviews{
                
                iv.hidden = true
            }
            
            /// 遍历图片数据
            var index: Int = 0
            
            for v in pic_urls ?? []{
                
          let imgView = subviews[index++] as! UIImageView
                
                //赋值给每个图片
        imgView.xl_setImageUrl(v.thumbnail_pic, placeHoder: nil)
                
                imgView.hidden = false
                
                
                if index == 2 && pic_urls?.count == 4 {
                    
                    index++
                }
                
            }
            
            
        }
        
        
        
    }
    /// 更新图像尺寸
    
    func updataImageSize(size: CGSize){
        
        let iv = subviews[0]
        
        if pic_urls?.count == 1{
            
            iv.frame = CGRect(origin: CGPointZero, size: size)
            
            return
        }
        
        iv.frame = CGRect(origin: CGPointZero, size: StatusCellLayout.pictureSize)
        
    }
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //设置界面
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 私有方法
    /// 首页图片选择调用的方法
  @objc  private func imgTapClick(recigest: UIGestureRecognizer){
        
    
      var index = recigest.view!.tag
    
     if pic_urls?.count==4 && index > 2 {
        
        index--
     }
    
    //获取对应图像的url
//    print(pic_urls?[index].bmiddle_pic)
    
    guard let pic_urls = pic_urls else{
        
        return
    }
    
    let urls = (pic_urls as NSArray).valueForKey("bmiddle_pic")
    
 //发送通知
    
    NSNotificationCenter.defaultCenter().postNotificationName(XLSelectPictureNotication, object: self, userInfo: [XLPictureSelectKey: index,XLPictureUrlKey:urls])
    
    
    
    }
    
    
    
    
    
}
// MARK: - 设置界面
extension StatusPictureView{
    
    private func setupUI(){
        
        backgroundColor = superview?.backgroundColor
        //超出父控件 全部拆掉
        clipsToBounds = true
        
        //每张图像的大小
        let rect = CGRect(origin: CGPointZero, size: StatusCellLayout.pictureSize)
        
        //偏移量
        
        let move = StatusCellLayout.pictureSize.width + StatusCellLayout.pictureMargin
        
        //添加视图
        
        for i in 0..<9{
            
            let row = CGFloat (i / Int(StatusCellLayout.pictureRow))
            
            let col = CGFloat(i % Int(StatusCellLayout.pictureRow))
            
            let iv = UIImageView()
           // iv.backgroundColor = UIColor.redColor()
            
            iv.contentMode = .ScaleAspectFill
            iv.clipsToBounds = true
            
            addSubview(iv)
            
            iv.frame = CGRectOffset(rect, move*col, move*row)
            //用户交互设置
            iv.userInteractionEnabled = true
            iv.tag = i
             //添加手势
            let tap = UITapGestureRecognizer(target: self, action: "imgTapClick:")
            iv.addGestureRecognizer(tap)
            
            
            
        }
        
        
        
    }
    
}
